package com.hitqz.robot.biz.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.entity.GoodsBuffersEntity;
import com.hitqz.robot.api.business.entity.RobotMapPosEntity;
import com.hitqz.robot.api.business.params.AddOrUpdateGoodsBufferParams;
import com.hitqz.robot.api.business.params.GoodsBufferParams;
import com.hitqz.robot.api.business.vo.GoodsBufferVo;
import com.hitqz.robot.biz.service.GoodsBuffersService;
import com.hitqz.robot.biz.service.RobotMapPosService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xupkun
 * @date 2025/1/3
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/goodsBuffer")
@Tag(name = "缓存架管理")
@Slf4j
//@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class GoodsBufferController {

    private final GoodsBuffersService goodsBuffersService;

    private final RobotMapPosService robotMapPosService;

    @Operation(summary = "获取缓存架数据")
    @GetMapping
    public R<List<GoodsBufferVo>> list(@ParameterObject GoodsBufferParams params) {
        LambdaQueryWrapper<GoodsBuffersEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq( GoodsBuffersEntity::getGroupId, params.getGroupId());
        List<GoodsBuffersEntity> result = goodsBuffersService.list(wrapper);
        List<GoodsBufferVo> robotVos = Lists.newArrayList();
        result.forEach(t-> {
            GoodsBufferVo vo = new GoodsBufferVo();
            BeanUtil.copyProperties(t, vo, false);
            robotVos.add(vo);
        });
        return R.ok(robotVos);
    }


    @Operation(summary = "添加缓存架数据")
    @PutMapping
    public R add(@RequestBody AddOrUpdateGoodsBufferParams params) {

        GoodsBuffersEntity goodsBuffersEntity = goodsBuffersService.getOne(Wrappers.<GoodsBuffersEntity>lambdaQuery()
                        .eq(GoodsBuffersEntity::getGroupId, params.getGroupId())
                .eq(GoodsBuffersEntity::getName, params.getName()).last(" limit 1")
        );
        if (goodsBuffersEntity != null) {
            return R.failed("缓存架名称重复");
        }
        goodsBuffersEntity = new GoodsBuffersEntity();
        BeanUtil.copyProperties(params, goodsBuffersEntity, false);
        RobotMapPosEntity robotMapPos = robotMapPosService.getById(goodsBuffersEntity.getBindPosId());
        goodsBuffersEntity.setBindPosName(robotMapPos.getName());
        goodsBuffersService.save(goodsBuffersEntity);
        return R.ok();
    }




    @Operation(summary = "修改缓存架数据")
    @PostMapping
    public R update(@RequestBody AddOrUpdateGoodsBufferParams params) {
        GoodsBuffersEntity goodsBuffersEntity = goodsBuffersService.getOne(Wrappers.<GoodsBuffersEntity>lambdaQuery()
                .eq(GoodsBuffersEntity::getGroupId, params.getGroupId())
                        .ne(GoodsBuffersEntity::getId, params.getId())
                .eq(GoodsBuffersEntity::getName, params.getName()).last(" limit 1")
        );
        if (goodsBuffersEntity != null) {
            return R.ok("缓存架名称重复");
        }
        goodsBuffersEntity = goodsBuffersService.getById(params.getId());
        BeanUtil.copyProperties(params, goodsBuffersEntity, false);
        RobotMapPosEntity robotMapPos = robotMapPosService.getById(goodsBuffersEntity.getBindPosId());
        goodsBuffersEntity.setBindPosName(robotMapPos.getName());
        goodsBuffersService.updateById(goodsBuffersEntity);
        return R.ok();
    }


    @DeleteMapping
    @Operation(summary = "删除缓存架数据")
    public R removeById(@RequestBody Long[] ids) {
        goodsBuffersService.removeByIds(CollUtil.toList(ids));
        return R.ok();
    }


    @Operation(summary = "根据id查询详情")
    @GetMapping("/{id}")
    public R<GoodsBufferVo> getById(@PathVariable Long id) {
        GoodsBuffersEntity goodsBuffers = goodsBuffersService.getById(id);
        GoodsBufferVo robotVos = new GoodsBufferVo();
        BeanUtil.copyProperties(goodsBuffers, robotVos, false);
        return R.ok(robotVos);
    }

}
